<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日期范围">
        <el-date-picker v-model="queryParams.dateRange" type="datetimerange" range-separator="至"
                        start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd"
                        class="small-date-picker" />
      </el-form-item>
      <el-form-item label="产品ID" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入产品ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="uid">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status"  clearable placeholder="请选择状态" filterable>
            <el-option v-for="(item, i) in extData.statusList || []" :key="i" :label="item.key"
                       :value="item.value"></el-option>
          </el-select>

        </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productUnionList" >
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品ID" align="center" prop="productId" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="用户ID" align="center" prop="uid">
        <template slot-scope="scope">
          <span class="clickable-uid" @click="handleDetailsDialog(scope.row)">{{ scope.row.uid }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="mobile" width="120"/>
      <el-table-column label="登陆地址" align="center" prop="loginUrl">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.loginUrl" placement="top-start"
            :disabled="!scope.row.loginUrl || scope.row.loginUrl.length <= 20">
            <div v-if="scope.row.loginUrl">{{ scope.row.loginUrl.length <= 20 ? scope.row.loginUrl :
              scope.row.loginUrl.slice(0, 20) + '...' }}</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="90" />
      <el-table-column label="返回值" align="center" prop="code" />
      <el-table-column label="返回信息" align="center" prop="message" />
      <el-table-column label="返回参数" align="center" prop="paramInfo">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.paramInfo" placement="top-start"
            :disabled="!scope.row.paramInfo || scope.row.paramInfo.length <= 20">
            <div v-if="scope.row.paramInfo">{{ scope.row.paramInfo.length <= 20 ? scope.row.paramInfo :
              scope.row.paramInfo.slice(0, 20) + '...' }}</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="返回结果" align="center" prop="resultInfo">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.resultInfo" placement="top-start"
            :disabled="!scope.row.resultInfo || scope.row.resultInfo.length <= 20">
            <div v-if="scope.row.resultInfo">{{ scope.row.resultInfo.length <= 20 ? scope.row.resultInfo :
              scope.row.resultInfo.slice(0, 20) + '...' }}</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="过期时间" align="center" prop="expireTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:productUnion:edit']"
          >查看</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品联登结果信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="登陆地址" prop="loginUrl">
          <el-input v-model="form.loginUrl" type="textarea"  />
        </el-form-item>
        <el-form-item label="返回值" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="返回信息" prop="message">
          <el-input v-model="form.message"  />
        </el-form-item>
        <el-form-item label="返回参数" prop="paramInfo">
          <el-input v-model="form.paramInfo" type="textarea" />
        </el-form-item>
        <el-form-item label="返回结果" prop="resultInfo">
          <el-input v-model="form.resultInfo" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户详情弹窗 -->
    <Member ref="MemberRef" />
  </div>
</template>

<style scoped>
.clickable-uid {
  cursor: pointer;
  color: blue;
}
</style>
<script>
import { listProductUnion, getProductUnion, delProductUnion, addProductUnion, updateProductUnion,unionInfoExtData } from "@/api/product/productUnion";
import dayjs from "dayjs";
import Member from "@/views/member/memeber.vue";

export default {
  name: "ProductUnion",
  components: {Member},
  data() {
    return {
      extData: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品联登结果信息表格数据
      productUnionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateInfo: null,
        productId: null,
        productName: null,
        uid: null,
        mobile: null,
        loginUrl: null,
        status: null,
        code: null,
        message: null,
        paramInfo: null,
        resultInfo: null,
        expireTime: null,
        startTime: null,
        endTime: null,
        dateRange: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dateInfo: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        productId: [
          { required: true, message: "产品ID不能为空", trigger: "blur" }
        ],
        productName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
        uid: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getOptions();
  },
  methods: {
    getOptions() {
      unionInfoExtData().then((response) => {
        console.log(response.data);
        this.extData = response.data;
      });
    },
    /** 查询产品联登结果信息列表 */
    getList() {
      this.loading = true;
      const sata = { ...this.queryParams }
      if (sata.dateRange && sata.dateRange.length === 2) {
        const startTime = this.queryParams.dateRange[0];
        const endTime = this.queryParams.dateRange[1];
        sata.startTime = startTime;
        sata.endTime = endTime;
        sata.dateRange = undefined;
        delete sata.dateRange;
      }
      listProductUnion(sata).then(response => {
        this.productUnionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dateInfo: null,
        productId: null,
        productName: null,
        uid: null,
        mobile: null,
        loginUrl: null,
        status: null,
        code: null,
        message: null,
        paramInfo: null,
        resultInfo: null,
        expireTime: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    async handleDetailsDialog(row) {
      this.$refs.MemberRef.show(row.uid)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品联登结果信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProductUnion(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品联登结果信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProductUnion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductUnion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除产品联登结果信息编号为"' + ids + '"的数据项？').then(function() {
        return delProductUnion(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/productUnion/export', {
        ...this.queryParams
      }, `productUnion_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
